<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="订单编号" prop="outTradeNo">
                <el-input v-model="queryParams.outTradeNo" placeholder="请输入订单编号" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="雪场名称" prop="snowFieldName">
                <el-input v-model="queryParams.snowFieldName" placeholder="请输入雪场名称" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="订单状态" prop="orderStatus">
                <el-select v-model="queryParams.orderStatus" placeholder="请选择订单状态">
                    <el-option v-for="item in statusList" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
                    v-hasPermi="['ski:order:list']">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                    v-hasPermi="['ski:order:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['ski:order:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="商户订单号" align="center" prop="outTradeNo" show-overflow-tooltip />
            <el-table-column label="雪场名称" align="center" prop="mealSnowName" show-overflow-tooltip />
            <el-table-column label="套餐名称" align="center" prop="setMealName" show-overflow-tooltip />
            <el-table-column label="订单金额" align="center" prop="total" width="150" />
            <el-table-column label="订单状态" align="center" width="120">
                <template slot-scope="scope">
                    <el-tag size="small" v-if="scope.row.orderStatus == 'paid'">已支付</el-tag>
                    <el-tag type="info" size="small" v-if="scope.row.orderStatus == 'canceled'">已取消</el-tag>
                    <el-tag type="success" size="small" v-if="scope.row.orderStatus == 'completed'">完成</el-tag>
                    <el-tag type="warning" size="small" v-if="scope.row.orderStatus == 'applyRefund'">申请退款</el-tag>
                    <el-tag type="danger" size="small" v-if="scope.row.orderStatus == 'refuseRefund'">拒接退款</el-tag>
                    <el-tag type="success" size="small" v-if="scope.row.orderStatus == 'agreeRefund'">同意退款</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="下单时间" align="center" prop="orderTime" width="150">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.orderTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" width="150">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-view" @click="handleDetail(scope.row)"
                        v-hasPermi="['ski:order:query']">详情</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['ski:order:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />

        <!-- 添加或修改订单对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
            <el-form ref="form" :model="form" label-width="100px">
                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-form-item label="订单编号">
                            <el-input v-model="mealForms.outTradeNo" />
                        </el-form-item>
                        <el-form-item label="用户名">
                            <el-input v-model="mealForm.memberName" />
                        </el-form-item>
                        <el-form-item label="订单状态">
                            <el-tag size="small" v-if="mealForms.orderStatus == 'paid'">已支付</el-tag>
                            <el-tag type="info" size="small" v-if="mealForms.orderStatus == 'canceled'">已取消</el-tag>
                            <el-tag type="success" size="small" v-if="mealForms.orderStatus == 'completed'">完成</el-tag>
                            <el-tag type="warning" size="small" v-if="mealForms.orderStatus == 'applyRefund'">申请退款</el-tag>
                            <el-tag type="danger" size="small" v-if="mealForms.orderStatus == 'refuseRefund'">拒接退款</el-tag>
                            <el-tag type="success" size="small" v-if="mealForms.orderStatus == 'agreeRefund'">同意退款</el-tag>
                        </el-form-item>
                        <el-form-item label="订单金额">
                            <el-input v-model="mealForm.setMealPrice" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="雪场名称">
                            <el-input v-model="mealForm.snowFieldName" />
                        </el-form-item>
                        <el-form-item label="套餐名称">
                            <el-input v-model="mealForm.setMealName" />
                        </el-form-item>
                        <el-form-item label="下单时间">
                            <el-input v-model="mealForms.orderTime" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-form-item label="更多服务">
                    <el-tag v-for="(item, index) in options" :key="index" style="margin-right:10px;">
                    {{ item.servicesName }}</el-tag>
                </el-form-item>
            </el-form>

        </el-dialog>
    </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder } from "@/api/business/order";
import { getAllService, listMeal } from "@/api/business/meal";
export default {
    name: "Order",
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 订单表格数据
            orderList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                outTradeNo: null,
                snowFieldName: null,
                orderStatus: null,
                isAsc: 'desc',
                orderByColumn: 'createTime',
            },
            // 套餐表单参数
            mealForm: {},
            mealForms: {},
            form: {},
            //更多服务数据
            options: [],
            statusList: [
                { value: 'paid', label: '已支付' },
                { value: 'canceled', label: '已取消' },
                { value: 'completed', label: '完成' },
                { value: 'applyRefund', label: '申请退款' },
                { value: 'refuseRefund', label: '拒接退款' },
                { value: 'agreeRefund', label: '同意退款' },
            ],
            setMealList: []
        };
    },
    created() {
        this.getList();
    },
    methods: {
        // 查询套餐
        getMeal() {
            listMeal({ pageNum: 1, pageSize: 10, },).then(response => {
                this.setMealList = response.rows;
            });
        },
        /** 查询订单列表 */
        getList() {
            this.loading = true;
            listOrder(this.queryParams).then(response => {
                this.orderList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },

        /**查询服务信息列表 */
        getAllService() {
            getAllService().then(response => {
                // console.log('response----------: ', response)
                this.options = response.data
            })
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {};
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.orderId)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },

        /**详情按钮操作 */
        handleDetail(row) {
            this.reset();
            this.getAllService()
            getOrder(row.orderId).then(response => {
                console.log('response--------: ', response)
                this.mealForm = response.data.orderDetail;
                this.mealForms = response.data.order;
                let ids = response.data.orderDetailServices.map(v => v.servicesId);
                this.$set(this.form, 'servicesIdList', ids)
                this.open = true;
                this.title = "订单信息详情";
            });
        },

        /** 删除按钮操作 */
        handleDelete(row) {
            const orderIds = row.orderId || this.ids;
            this.$modal.confirm('是否确认删除订单编号为"' + orderIds + '"的数据项？').then(function () {
                return delOrder(orderIds);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('/system/order/export', {
                ...this.queryParams
            }, `order_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>
